using PetShop.Domain.Model;


using PetShop.Web.Controls;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PetShop.Web {
	public class EditAccount : Page {
	
		protected Label lblUserId;
		protected TextBox txtEmail;
		protected AddressUI addr;
		protected NavBar header;
		protected System.Web.UI.WebControls.RequiredFieldValidator valEmail;
		protected System.Web.UI.WebControls.ImageButton btnSubmit;
		protected System.Web.UI.HtmlControls.HtmlForm frmEditAcct;
		protected Preferences prefs;
		
		override protected void OnLoad(EventArgs e) {
			if (!IsPostBack) {

				ProcessFlow.AccountController accountController = new ProcessFlow.AccountController();

				// Retrieve the account information from the account controller
				Account myAccount = accountController.GetAccountInfo(true);
				
				lblUserId.Text = myAccount.UserId;
				txtEmail.Text = myAccount.Email;
				addr.Address = myAccount.Address;
				prefs.Language = myAccount.LangPref;
				prefs.Category = myAccount.FavCategory.CategoryID;
				prefs.IsShowBanners = myAccount.IsShowBanners;
				prefs.IsShowFavorites = myAccount.IsShowFavorites;				
			}
		}

		private void InitializeComponent() {
		}

        protected void SubmitClicked(object sender, ImageClickEventArgs e)
        {
            if (Page.IsValid)
            {

                ProcessFlow.AccountController accountController = new ProcessFlow.AccountController();

                // Retrieve the account information from the account controller
                Account myAccount = accountController.GetAccountInfo(true);

                // Merge in the changes
                myAccount.Email = WebComponents.CleanString.InputText(txtEmail.Text, 50);
                myAccount.Address = addr.Address;
                myAccount.LangPref = prefs.Language;
                PetShop.Domain.Model.Category cat = new PetShop.Domain.Model.Category();
                cat.CategoryID = prefs.Category;
                myAccount.FavCategory = cat;
                myAccount.MyListOpt = System.Convert.ToInt32(prefs.IsShowFavorites);
                myAccount.BannerOpt = System.Convert.ToInt32(prefs.IsShowBanners);


                // Submit the changes back to the controller
                accountController.UpdateAccount(myAccount);
            }
        }
	}
}